from pymt import *
from OpenGL.GL import *
import xml.dom.minidom
import os
current_dir = os.path.dirname(__file__)

class TransformToXML():

    def __init__(self, listPages, listMiniatures):
		self.doc = xml.dom.minidom.Document()
		self.listPages=listPages
		self.listMiniatures=listMiniatures
		self.build(self.doc)
		self.write_to_file(self.doc) 
	
    def build(self, doc):
		story = doc.createElementNS(None, "story")
		story.setAttributeNS(None,"id","1")
		story.setAttributeNS(None,"title","")
		story.setAttributeNS(None,"author","")		
		doc.appendChild(story)	
		
		i=0
		while(i<len(self.listMiniatures)):
			for mini in self.listMiniatures:
				
				if (mini._get_emplacement()==i):
					page = doc.createElementNS(None,'page')
					page.setAttributeNS(None,"id",str(mini._get_id()))		
					story.appendChild(page)
					
					html = doc.createElementNS(None,'html')
					page.appendChild(html)
						
					head = doc.createElementNS(None,'head')
					html.appendChild(head)
					
					meta1=doc.createElementNS(None, 'meta')
					meta1.setAttributeNS(None,"http-equiv","content-type")
					meta1.setAttributeNS(None,"content","text/html; charset=utf-8")		
					head.appendChild(meta1)
				
					meta2=doc.createElementNS(None, 'meta')
					meta2.setAttributeNS(None,"name","viewport")
					meta2.setAttributeNS(None,"content","width=704px, user-scalable=no")		
					head.appendChild(meta2)
				
					style=doc.createElementNS(None, 'style')
					head.appendChild(style)		
					style.appendChild(doc.createTextNode("@font-face { \
					font-family: \'Traveling_TypewriterRegular';\
					src: url(\'travelingtypewriter-webfont.eot');\
					src: local(\'?\'), url(\'travelingtypewriter-webfont.woff\') format(\'woff\'), url(\'travelingtypewriter-webfont.ttf\') format(\'truetype\'), url(\'travelingtypewriter-webfont.svg#webfontELQf6xwY\') format(\'svg\');\
					font-weight: normal;\
					font-style: normal;\
					}"))

					script=doc.createElementNS(None, 'script')
					script.setAttributeNS(None,"type","text/javascript")		
					head.appendChild(script)		
					script.appendChild(doc.createTextNode("function fullScreenImage(elt) {\
					document.location.replace(\"ICEQUERY.image.\"+elt);}\
					function fullScreenText(elt) {\
					document.location.replace(\"ICEQUERY.text.\"+elt);\
					}"))
				
					body=doc.createElementNS(None, 'body')
					body.setAttributeNS(None,"style","margin-top=44px;")		
					html.appendChild(body)
					i+=1
					
					self.page=self.listPages[mini._get_id()]
					x=0
					for textZone in self.page._get_text_zones():				
						text=doc.createElementNS(None, 'div')
						text.setAttributeNS(None,"id",str(x))
						text.setAttributeNS(None,"class","text")
						text.setAttributeNS(None,"style","text-align:justify;\
						position:absolute;\
						top:"+str(self.page.height-textZone._get_top())+";\
						left:"+str(textZone._get_left())+"; \
						width:"+str(textZone._get_width())+";\
						height:"+str(textZone._get_height())+";")
						text.setAttributeNS(None,"onclick", "fullScreenText(\'"+ str(x) +"\')")
						text.appendChild(doc.createTextNode(textZone._get_content()._get_label()))
						body.appendChild(text)
						x+=1

					for mediaZone in self.page._get_media_zones():
						img=doc.createElementNS(None, 'div')
						img.setAttributeNS(None,"id",str(x))
						img.setAttributeNS(None,"class","image")
						img.setAttributeNS(None,"style","text-align:justify;\
						position:absolute;\
						top:"+str(self.page.height-mediaZone._get_top())+";\
						left:"+str(mediaZone._get_left())+"; \
						width:"+str(mediaZone._get_width())+";\
						height:"+str(mediaZone._get_height())+";")
						img.setAttributeNS(None,"onclick","fullScreenText(\'"+str(x)+"\')")		
						body.appendChild(img)						
		
						src=doc.createElementNS(None, 'img')
						src.setAttributeNS(None,"src",str(mediaZone._get_content()._get_filename()))
						src.setAttributeNS(None,"alt","")
						img.appendChild(src)
						
						x+=1						
		
		return story		

    def write_to_file(self, doc):
		f = open(current_dir+'/xml/essai.xml', 'w')
		f.write(doc.toprettyxml())
		f.close()